package com.wuxiaoqin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuxiaoqin.server.entity.Address;
import com.wuxiaoqin.server.entity.Users;
import com.wuxiaoqin.server.mapper.AddressMapper;
import com.wuxiaoqin.server.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuxiaoqin.server.service.IUsersService;
import com.wuxiaoqin.uitls.respObj.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-10-29
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Autowired(required = false)
    private AddressMapper addressMapper;

    @Autowired
    private IUsersService usersService;
    public Address selectddd(){
        Address address = addressMapper.selectById(2);
        return address;
    }

    /**
     * 查询用户地址
     * @param id
     * @return
     */
    @Override
    public List<Address> getAddress(Integer id) {
        List<Address> add_list = addressMapper.selectList(new QueryWrapper<Address>().eq("ADD_userid", id));
        return add_list;
    }

    /**
     * 修改地址
     * @param userid
     * @param addressid
     * @param address
     * @param name
     * @param phone
     * @return
     */
    @Override
    public RespBean addressModify(Integer userid, String addressid, String address, String name, String phone) {
        Address address1 = addressMapper.selectOne(new QueryWrapper<Address>()
                .eq("ADD_id", addressid).eq("ADD_userid", userid));
        if (address1 == null){
            return RespBean.error("地址信息为空");
        }
        address1.setAddAddress(address);
        address1.setAddName(name);
        address1.setAddPhone(phone);
        addressMapper.updateById(address1);
        return RespBean.success("修改成功");
    }

    /**
     * 修改默认地址
     *
     * @param uId
     * @param addressid
     * @return
     */
    @Override
    public RespBean addStateModify(Integer uId, Integer addressid) {
        Address address0 = addressMapper.selectOne(new QueryWrapper<Address>()
                .eq("ADD_id", addressid).eq("ADD_userid", uId));
        if (address0 == null){
            return RespBean.error("您输入地址信息为空");
        }
        Address address1 = addressMapper.selectOne(new QueryWrapper<Address>().eq("ADD_userid", address0.getAddUserid())
                .eq("ADD_state", 0));

        address1.setAddState(1);
        addressMapper.updateById(address1);

        address0.setAddState(0);
        addressMapper.updateById(address0);
        return RespBean.success("修改成功");
    }

    /**
     * 默认地址是0
     * @param userid
     * @param address
     * @param name
     * @param phone
     * @param state
     * @return
     */
    @Override
    public RespBean addAddress(Integer userid, String address, String name, String phone, Integer state) {
        Integer count = addressMapper.selectCount(new QueryWrapper<Address>().eq("ADD_userid", userid));
        if (count >= 3){
            return RespBean.error("地址已经超出限制数量");
        }

        Address add = new Address();
        add.setAddUserid(userid);
        add.setAddAddress(address);
        add.setAddName(name);
        add.setAddPhone(phone);
        add.setDeleted(0);
        if(state == 0){
            if (count > 0){
                Address address1 = addressMapper.selectOne(new QueryWrapper<Address>().eq("ADD_userid", userid)
                        .eq("ADD_state", 0));
                address1.setAddState(1);
                addressMapper.updateById(address1);
            }
            add.setAddState(0);
        }else {
            add.setAddState(1);
        }
        addressMapper.insert(add);
        return RespBean.success("添加成功");
    }

    @Override
    public RespBean deleteAddress(Integer addressId) {
        Users currentUser = usersService.getCurrentUser();
        Address address = addressMapper.selectOne(new QueryWrapper<Address>().eq("ADD_userid", currentUser.getUId()).eq("ADD_id", addressId));
        if (address==null){
            RespBean.error("该用户没有该购物车");
        }
        addressMapper.deleteById(addressId);
        return RespBean.success("删除成功");
    }
}
